// pages/register/register.js
var util = require('../../utils/util.js')
var app = getApp()
var rootUrl = app.globalData.rootDocment
var imgPublicUrl = rootUrl + 'Public/Home/images/'
Page({
  /**
   * 页面的初始数据
   */
  data: {
	  sendmsgclass: "", //验证码按钮class
	  getmsg: "获取验证码", //验证码按钮标题
	  sendtp:true,	//发送状态
	  verifcode: 0,	//服务器验证码
	  phdisabletp: false, 	//手机号input是否锁定
	  telPhoneNumber: "",
	  verificationCode:0,	//本地验证码
	  passWord: "",
    topid: 0, //上级用户id
	  confirmPassword:"",
	  submitload: false,	//提交Loading状态
	  backcolor: '#ccc', //是否能点击
    flag: false,
	  imageArr: {
		  loginPIocn: imgPublicUrl + 'loginPIocn.png',
		  verificationIcon: imgPublicUrl + 'verificationIcon.png',
		  loginMIcon: imgPublicUrl + 'loginMIcon.png',
		  confirmIcon: imgPublicUrl + 'confirmIcon.png',
		  loginBottonImg: imgPublicUrl + 'loginBottonImg.png'
	  }  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var scodeval = app.globalData.scodeval
    if (scodeval){
      if (scodeval.indexOf(rootUrl) >= 0 && scodeval.indexOf('user') >= 0) {  //判断是否规范
        var scoderes = scodeval.replace(rootUrl, '')
        var strvals = parseInt(scoderes.replace(/[^0-9]/ig, ""))
        if (strvals > 0) {
          this.setData({ topid: strvals })
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  app.publicchangenavigate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	return app.globalshare();
  },
  //手机号
  telPhoneNumberInput: function (e) {
    this.setData({
      telPhoneNumber: e.detail.value,
    })
    if (this.data.telPhoneNumber && this.data.verificationCode && this.data.passWord && this.data.confirmPassword) {
      this.setData({ backcolor: "#63aeff" })
    } else {
      this.setData({ backcolor: "#ccc", flag: false })
    }
  },
  //验证码
  verificationCodeInput: function (e) {
    this.setData({
      verificationCode: e.detail.value,
    })
    if (this.data.telPhoneNumber && this.data.verificationCode && this.data.passWord && this.data.confirmPassword) {
      this.setData({ backcolor: "#63aeff" })
    } else {
      this.setData({ backcolor: "#ccc", flag: false})
    }
  },
  //密码
  passWordInput: function (e) {
    this.setData({
      passWord: e.detail.value,
    })
    if (this.data.telPhoneNumber && this.data.verificationCode && this.data.passWord && this.data.confirmPassword) {
      this.setData({ backcolor: "#63aeff" })
    } else {
      this.setData({ backcolor: "#ccc", flag: false })
    }
  },
  //确认密码
  confirmPasswordInput: function (e) {
    this.setData({
      confirmPassword: e.detail.value,
    })
    if (this.data.telPhoneNumber && this.data.verificationCode && this.data.passWord && this.data.confirmPassword) {
      this.setData({ backcolor: "#63aeff" })
    } else {
      this.setData({ backcolor: "#ccc", flag: false })
    }
  },
  //点击提交按钮
  formSubmit: function (e) {
    var warn = "";
    var that = this;
    if (e.detail.value.telPhoneNumber == "") {
      warn = "请填写您的手机号！";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.telPhoneNumber))) {
      warn = "手机号格式不正确";
    } else if (e.detail.value.verificationCode == '') {
      warn = "请输入验证码";
	  } else if (e.detail.value.verificationCode != that.data.verifcode) {	//	验证码是否正确
		  warn = "验证码错误";
	  } else if (e.detail.value.passWord == '') {
      warn = "请输入密码";
    } else if ((/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/.test(e.detail.value.passWord))){
      warn = "密码由字母和数字组成，至少6位";
    } else if (e.detail.value.confirmPassword == '') {
      warn = "请再次输入密码";
    } else if (e.detail.value.passWord != e.detail.value.confirmPassword) {
      warn = "两次密码不一致";
    } else {
      that.setData({
        flag : true
      })
    }
	 //执行结果
	 if (that.data.flag) {
		 //开启加载
		 that.setData({submitload:true})

		 var sessvalue = wx.getStorageSync('sessionid')
		 //整合上传数据
		 e.detail.value.nickName = app.globalData.userInfo.nickName
		 e.detail.value.avatarUrl = app.globalData.userInfo.avatarUrl
		 e.detail.value.openids = sessvalue
     e.detail.value.topid = that.data.topid
		//提交给服务端注册信息
		 util.AJAX('Home/Login/registeredusers', e.detail.value , function (res) { //方法体
			 //关闭加载
			 that.setData({ submitload: false })

			 if (res.data.StatusCode == 100) {  //成功
				wx.showToast({
					title: res.data.Msg,
					icon: 'success',
					duration: 2000
				})

				setTimeout(function () { 
					//回退登录。。。
					wx.navigateBack()
				}, 2000);
			} else {	//失败
				wx.showModal({
					title: '提示',
					content: res.data.Msg
				})
			}
		}, 'POST')

	 } else {
      wx.showModal({
        title: '提示',
        content: warn
      })
    }
  },
  //获取手机验证码
  getVerifCode : function (e) {
	  var that = this
	  var timer = that.data.sendtp
	  if (timer) {
		  var telPhone = that.data.telPhoneNumber
		  //验证手机是否为空
		  if (telPhone == "") {
			  wx.showModal({
				  title: '提示',
				  content: "请填写您的手机号！"
			  })
		  } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(telPhone))) {
			  wx.showModal({
				  title: '提示',
				  content: "手机号格式不正确"
			  })
		  } else{
			  var time = 120  //倒计时总时间
			  that.setData({
				  sendmsgclass: "active",
				  getmsg: "发送中...",
				  sendtp: false
			  })

			  //去服务端验证是否过期
			  util.AJAX('Home/Login/sendPhoneCode', { mobile: telPhone }, function (res) { //方法体
				  if (res.data.status == 100) {  //发送成功
					  //锁定手机号
					  //更改验证码值
					  that.setData({
						  verifcode: res.data.verify,
						  phdisabletp: true
					  })
					  //开始计时
					  var inter = setInterval(function () {
						  that.setData({
							  sendmsgclass: "active",
							  getmsg: time + "s",
						  })
						  time--
						  if (time < 0) {
							  clearInterval(inter)
							  that.setData({
								  sendmsgclass: "",
								  getmsg: "获取验证码",
								  sendtp: true
							  })
						  }
					  }, 1000)
				  } else {	//发送失败
					  that.setData({
						  sendmsgclass: "",
						  getmsg: "获取验证码",
						  sendtp: true
					  })
				  }
			  }, 'POST')
		  }
		  
	  }
  }
  
})